package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppConvert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;
@Service
public class AppServiceImpl implements AppService {

    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    AppMapper appMapper;


    /**
     * @param merchantId 商户Id
     * @param appDTO     应用信息
     * @return 新添加的应用信息
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {

        //参数校验
        if (merchantId == null || appDTO == null || StringUtil.isBlank(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        // 未通过资质审核不能创建应用
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (!"2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }

        //应用名称唯一性校验
        if (isExistsAppName(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }

        //将dto转换为entity
        App app = AppConvert.INSTANCE.dto2entity(appDTO);

        //设置商户Id
        app.setMerchantId(merchantId);

        //设置appId
        app.setAppId(UUID.randomUUID().toString());
        appMapper.insert(app);

        //将app转换为appDto返回
        AppDTO appDTO1 = AppConvert.INSTANCE.entity2dto(app);
        return appDTO1;
    }

    /**
     *   判断应用名称是否存在
     * @param appName  应用名
     * @return 真,假
     */
    private Boolean isExistsAppName(String appName){
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appName));
        return count > 0;
    }


    /**
     * 查询商户的应用列表
     *
     * @param merchantId 商户Id
     * @return 该商户的应用列表
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> appList = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        List<AppDTO> appDTOS = AppConvert.INSTANCE.listEntity2dto(appList);
        return appDTOS;
    }

    /**
     * 根据应用Id查询应用信息
     *
     * @param appId 应用ID
     * @return 应用信息
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppById(String appId) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        AppDTO appDTO = AppConvert.INSTANCE.entity2dto(app);
        return appDTO;
    }

    /**
     * 校验应用是否属于该商户
     *
     * @param appId      应用Id
     * @param merchantId 商户ID
     * @return true:属于  false: 不属于
     * @throws BusinessException
     */
    @Override
    public Boolean isExist(String appId, Long merchantId) throws BusinessException {
        Integer app = appMapper.selectCount(new LambdaQueryWrapper<App>()
                .eq(App::getAppId, appId)
                .eq(App::getMerchantId,merchantId));
//        if (app>0) {
//            return true;
//        }
//        return false ;
        return app>0;
    }


}
